<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/28
 * Time: 12:39
 */

namespace app\admin\controller;


use app\admin\logic\ConfigLogic;
use think\App;
use think\Request;
use app\common\model\mysql\ConfigGroup;

class Config extends Base
{
    protected $logic;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->logic = new ConfigLogic();
    }

    public function index()
    {
        // 接收配置组传过来的配置组id
        $group_id = input("param.group_id", 0, "intval");
        $where    = [
            'group_id' => $group_id,
        ];

        try {
            $lists = $this->logic->getLists($where);
        } catch (\Exception $e) {
            return [];
        }

        return view('config/index', compact('group_id', 'lists'));
    }

    public function add()
    {
        $group_id = input("group_id", 0, "intval");
        if ($this->request->isPost()) {
            $data = input("post.");
            try {
                $result = $this->logic->addConfig($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            /** @var TYPE_NAME $result */
            if ($result) {
                $this->success($data['config_title']."添加成功", url('Config/index', ['group_id' => $group_id]));
            }
            $this->error("添加失败");
        } else {

            return view('config/add', compact('group_id'));
        }
    }

    public function edit()
    {
        $group_id = input("group_id", 0, "intval");
        if ($this->request->isPost()) {
            $id   = input("post.id", 0, "intval");
            $data = [
                'config_name'  => input('config_name', '', 'htmlspecialchars,strip_tags,strtoupper'),
                'config_title' => input('config_title', '', 'htmlspecialchars,strip_tags'),
                'config_value' => input('config_value', '', 'htmlspecialchars,strip_tags'),
                'config_type'  => input('config_type', 'text', 'htmlspecialchars,strip_tags'),
                'config_desc'  => input('config_desc', '', 'htmlspecialchars,strip_tags'),
                'config_info'  => input('config_info', '', 'htmlspecialchars,strip_tags'),
                'status'       => input('status', 1, 'intval'),
                'group_id'     => input('post.group_id', 0, 'intval'),
            ];
            try {
                $res = $this->logic->updateConfig($id, $data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if ($res) {
                $this->success($data['config_title'].'编辑成功', url('Config/index', ['group_id' => $group_id]));
            }
            $this->error('编辑失败');
        } else {
            // 接收点击编辑按钮传递的id
            $id         = input("param.id", 0, "intval");
            $config_one = $this->logic->getConfigOneById($id);
            if (empty($config_one)) {
                $this->error('该配置参数不存在');
            }

            return view('config/edit', compact('config_one'));
        }
    }

    public function delete(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(0, "请求类型不合法");
        }

        $id = $request->post('id', 0, 'intval');
        try {
            $result = $this->logic->deleteById($id);
        } catch (\Exception $e) {
            return writeJson(0, $e->getMessage());
        }

        if ($result) {
            return writeJson(1, "配置删除成功");
        }

        return writeJson(0, "配置删除失败");
    }

    /**
     * 网站配置
     * @return mixed
     * @throws \think\Exception
     */
    public function webConfig()
    {
        if ($this->request->isPost()) {
            $data = $_POST['config'];
            //            p($data);die;
            if (!is_array($data)) {
                $this->error('数据不能为空');
            }
            foreach ($data as $k => $v) {
                $this->logic->saveConfig($k, $v);
            }
            $this->success('修改成功');
        } else {
            // 获取配置组信息
            try {
                $config_groups = ConfigGroup::with(
                    [
                        'configs' => function ($query) {
                            $query->field(
                                'id,config_name,config_title,config_value,config_type,config_desc,group_id,config_info'
                            );
                        },
                    ]
                )->select()->toArray();
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            return view('config/web_config', compact('config_groups'));
        }
    }
}